package org.example.day20240819;

public class FenShu {
    long fenzi;
    long fenmu;

    public FenShu(long fenzi, long fenmu) {
        this.fenzi = fenzi;
        this.fenmu = fenmu;
    }

    @Override
    public String toString() {
        long result;
        if(Math.abs(this.fenzi)>Math.abs(this.fenmu)){
            result = this.fenzi/this.fenmu;
            if(this.fenzi <0 && this.fenmu >0 || this.fenzi >0 && this.fenmu <0){
                return result+" -"+Math.abs(this.fenzi)%Math.abs(this.fenmu)+"/"+this.fenmu;
            }else if(this.fenzi <0 && this.fenmu <0){
                return result+" "+Math.abs(this.fenzi)%Math.abs(this.fenmu)+"/"+Math.abs(this.fenmu);
            }else if(this.fenzi == 0){
                return String.valueOf(result);
            }
            return result+" "+Math.abs(this.fenzi)%Math.abs(this.fenmu)+"/"+this.fenmu;
        }
        else {
            return this.fenzi+"/"+this.fenmu;
        }
    }

    public boolean isOver(String fenShu){
        String[] result = fenShu.split("0");
        if(result.length>1){
            return true;
        }
        return false;
    }

    public String outPut(String fenshu){
        if(fenshu.length()>3)
        return fenshu.substring(0,1);
        return fenshu;
    }
    public long gcd() {
        long min_value = Math.abs(this.fenzi) < Math.abs(this.fenmu) ? Math.abs(this.fenzi) : Math.abs(this.fenmu);
        long gcd = 1;
        for (long i = min_value; i > 0; i--) {
            if (this.fenzi % i == 0 && this.fenmu % i == 0) {
                gcd = i;
                break;
            }
        }
        return gcd;
    }

    public void simple() {
        long gcd = this.gcd();
        this.fenmu /= gcd;
        this.fenzi /= gcd;
    }

    public FenShu add(FenShu fenShu) {
        long newFenmu = this.fenmu * fenShu.fenmu;
        long newFenzi = this.fenzi * fenShu.fenmu + fenShu.fenzi * this.fenmu;
        FenShu result = new FenShu(newFenzi, newFenmu);
        result.simple();
        return result;
    }
}